package com.clerkq.FlvPlayer.ui
{
//	import caurina.transitions.Tweener;
	
	import com.content.model.LoadedAssets.SkinProxy;
	import com.content.view.Component.Button.HighLightButton;
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import gs.TweenLite;
	
	public class ProgressBar extends UIObject implements IDisposable
	{
        public static const ON_DRAG:String = "PlayHead_Drag";
		public static const ON_PRESS_PLAYHEAD:String = "PlayHead_Press";
        public static const ON_RELEASE_PLAYHEAD:String = "PlayHead_Release";
        public static const ON_SEEK:String = "ProgressBar_Seeking";
        
		private const WIDTH:Number = 258;
		private const HEIGHT:Number = 25;
		
		private var _glowStick:Sprite;
		private var _playHead:HighLightButton;	// 滑条
		private var _bufferBar:Sprite;
		private var _seekPercent:Number;
		private var _playRegionMask:Sprite;
		private var _bufferRegionMask:Sprite;
		private var _timeIndicator:TextField;
		
		private var _dragStatus:Boolean;// = false
		
		
		public function ProgressBar()
		{
			super();
			_dragStatus = false;
			create();
			addEventListeners();
		}
		
		private function create():void
		{
			// Glow
			_glowStick = new SkinProxy.VideoPlayer_GlowStick() as Sprite;
			_glowStick.alpha = 0;
			addChild(_glowStick);
			
			// Played Region
			var playedRegion:Sprite = new SkinProxy.VideoPlayer_ProgressBarBG() as Sprite;
			addChild(playedRegion);
			_playRegionMask = new Sprite();
			_playRegionMask.graphics.beginFill(0xffff00, 1.0);
			_playRegionMask.graphics.drawRect(0,0,0.1,HEIGHT)
			addChild(_playRegionMask);
			playedRegion.mask = _playRegionMask;
			
			// Buffered Region
			_bufferBar = new SkinProxy.VideoPlayer_ProgressBarBG() as Sprite;
			_bufferBar.alpha = 0.4;
			addChild(_bufferBar);
			_bufferRegionMask = new Sprite();
			_bufferRegionMask.graphics.beginFill(0xffffff, 1.0);
			_bufferRegionMask.graphics.drawRect(0,0,0.1,HEIGHT);
			addChild(_bufferRegionMask);
			_bufferBar.mask = _bufferRegionMask;
			
			// Play head
			var sliderNormal:Sprite = new SkinProxy.VideoPlayer_Slider() as Sprite;
			var sliderLight:Sprite = new SkinProxy.VideoPlayer_SliderLight() as Sprite;
			_playHead = new HighLightButton(sliderNormal, sliderLight);
			_playHead.y = _playHead.height/2 - 2;
			addChild(_playHead);			
			
			// TimeIndicator
			var indicatorFmt:TextFormat = new TextFormat();
				indicatorFmt.font = "Arial"
				indicatorFmt.size = 11;
				indicatorFmt.color = 0xffffff;
				
			_timeIndicator = new TextField();
			_timeIndicator.defaultTextFormat = indicatorFmt;
			_timeIndicator.selectable = false;
			_timeIndicator.autoSize = TextFieldAutoSize.LEFT;
			_timeIndicator.antiAliasType = AntiAliasType.ADVANCED;
			addChild(_timeIndicator);
			_timeIndicator.text = " ";
			_timeIndicator.x = 265;
			_timeIndicator.y = (25-_timeIndicator.height)/2;
		}
		
		private function addEventListeners():void
		{
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_playHead.addEventListener(MouseEvent.MOUSE_DOWN, onPressPlayHead);
			_playHead.addEventListener(MouseEvent.MOUSE_UP, onReleasePlayHead);
			_bufferBar.addEventListener(MouseEvent.CLICK, onClickBufferBar);
		}
		
		// Events
		private function onMouseOver(evt:MouseEvent):void{
			
//			Tweener.addTween(_glowStick, {alpha:0.08, time:0.6, transition:"easeOutQuint"});
			TweenLite.to(_glowStick, 0.6, {alpha:0.08});
		}
		
		private function onMouseOut(evt:MouseEvent):void{
//			Tweener.addTween(_glowStick, {alpha:0.0, time:0.6, transition:"easeOutQuint"});
			TweenLite.to(_glowStick, 0.6, {alpha:0.0});
		}
		
		private function onPressPlayHead(evt:MouseEvent):void{
			_dragStatus = true;
			var bound:Rectangle = new Rectangle(0,_playHead.y,WIDTH,0);

			_playHead.startDrag(false, bound);
			stage.addEventListener(MouseEvent.MOUSE_UP, onReleasePlayHead);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			dispatchEvent(new Event(ProgressBar.ON_PRESS_PLAYHEAD, true));
		}
		
		private function onReleasePlayHead(evt:MouseEvent):void{
			_dragStatus = false;
			_playHead.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_UP, onReleasePlayHead);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			dispatchEvent(new Event(ProgressBar.ON_RELEASE_PLAYHEAD, true));			
		}
		
		private function onMouseMove(evt:MouseEvent):void{
			var sliderX:Number = _playHead.x;
			_playRegionMask.width = sliderX;
			dispatchEvent(new Event(ProgressBar.ON_DRAG, true));
		}
		
		private function onClickBufferBar(evt:MouseEvent):void{
			_seekPercent = evt.localX / WIDTH;
			dispatchEvent(new Event(ProgressBar.ON_SEEK, true));
		}
		
		// Get and Set
		public function get seekPercent():Number{		return _seekPercent;						}
		public function get dragPercent():Number{		return _playHead.x / WIDTH;		}
		
		// (0 ~ 1)
		public function set playPercent(percent:Number):void{
			if (!_dragStatus){
				var sliderX:Number = (percent) * WIDTH;
				_playHead.x = sliderX;
				_playRegionMask.width = sliderX;
			}
		}
		
		public function set bufferPercent(percent:Number):void{
			_bufferRegionMask.width = (percent) * WIDTH;
		}
		
		public function set displayText(text:String):void{
			_timeIndicator.text = text;
		}
		
		// Public /////////////////////////
		public function reset():void{
			bufferPercent = 0;
			playPercent = 0;
			displayText = "";
		}
		
		public function Dispose():void{
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			_playHead.removeEventListener(MouseEvent.MOUSE_DOWN, onPressPlayHead);
			_playHead.removeEventListener(MouseEvent.MOUSE_UP, onReleasePlayHead);
			_bufferBar.removeEventListener(MouseEvent.CLICK, onClickBufferBar);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onReleasePlayHead);
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			removeAllChilds();
		}

	}
}